package serviceGoods

import (
	"context"
	goods_pb "mxshop/api/goods/v1"
	"mxshop/app/mxshop_api/api/internal/data"
	dtoGoods "mxshop/app/mxshop_api/api/internal/domain/dto/goods"
	srvGoods "mxshop/app/mxshop_api/api/internal/service/goods"
)

type CategoryBrandService struct {
	data data.DataFactory
}

func (c CategoryBrandService) CategoryBrandList(ctx context.Context, req *goods_pb.CategoryBrandFilterRequest) (*dtoGoods.CategoryBrandDTOList, error) {
	var ret dtoGoods.CategoryBrandDTOList

	rsp, err := c.data.Goods().CategoryBrand().List(ctx, req)
	if err != nil {
		return nil, err
	}

	for _, v := range rsp.Items {
		ret.Items = append(ret.Items, &dtoGoods.CategoryBrandDTO{CategoryBrandDO: *v})
	}
	ret.TotalCount = rsp.TotalCount
	return &ret, nil
}

func (c CategoryBrandService) GetCategoryBrandList(ctx context.Context, ID int64) (*dtoGoods.BrandDTOList, error) {
	var ret dtoGoods.BrandDTOList

	rsp, err := c.data.Goods().CategoryBrand().Get(ctx, ID)
	if err != nil {
		return nil, err
	}

	for _, v := range rsp.Items {
		ret.Items = append(ret.Items, &dtoGoods.BrandDTO{
			Id:   v.Id,
			Name: v.Name,
			Logo: v.Logo,
		})
	}

	return &ret, nil
}

func (c CategoryBrandService) CreateCategoryBrand(ctx context.Context, req *dtoGoods.CategoryBrandDTO) (*dtoGoods.CategoryBrandDTO, error) {
	var ret dtoGoods.CategoryBrandDTO

	rsp, err := c.data.Goods().CategoryBrand().Create(ctx, &req.CategoryBrandDO)
	if err != nil {
		return nil, err
	}

	ret.Id = rsp.Id

	return &ret, nil
}

func (c CategoryBrandService) DeleteCategoryBrand(ctx context.Context, ID int64) error {
	return c.data.Goods().CategoryBrand().Delete(ctx, ID)
}

func (c CategoryBrandService) UpdateCategoryBrand(ctx context.Context, req *dtoGoods.CategoryBrandDTO) error {
	return c.data.Goods().CategoryBrand().Update(ctx, &req.CategoryBrandDO)
}

func newCategoryBrand(data data.DataFactory) srvGoods.CategoryBrandSrv {
	return &CategoryBrandService{data: data}
}
